package com.bbjob.service.impl;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bbjob.client.result.ResultForExamineeScoresDetail;
import com.bbjob.dto.Result;
import com.bbjob.mapper.DeliveryExamineeScoresDetailMapper;
import com.bbjob.model.DeliveryExamineeScoresDetail;
import com.bbjob.model.ExamineeScoresDetail;
import com.bbjob.query.ExamineeScoreDetailEntity;
import com.bbjob.query.QueryScoresDetail;
import com.bbjob.service.IExamineeScoresDetailService;
import com.bbjob.service.ISystemService;
import com.bbjob.toolutil.HttpClientUtil;
import com.bbjob.toolutil.Tool;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

@Service
public class ExamineeScoresDetailService implements
		IExamineeScoresDetailService {

	@Value("${oss.accessKeyId}")
	private String accessKeyId;
	@Value("${oss.accessKeySecret}")
	private String accessKeySecret;
	@Value("${oss.endpoint}")
	private String endpoint;
	@Value("${oss.bucket}")
	private String bucket;
	@Value("${sys.uniquecode}")
	private String uniquecode;
	@Value("${examol.center.url}")
	private String centerUrl;
	@Value("${examol.center.score.detail.list}")
	private String centerScoreDetailListUrl;
	@Value("${examol.center.score.detail.save}")
	private String centerScoreDetailSaveUrl;
	@Value("${examol.center.score.detail.clientSave}")
	private String centerScoreDetailClientSaveUrl;
	@Resource
	private ISystemService systemService;
	@Resource
	private DeliveryExamineeScoresDetailMapper deliveryExamineeScoresDetailMapper;

	@Override
	public Result selectByParams(Integer id, QueryScoresDetail detail) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uniquecode", uniquecode);
		params.put("pageNum", detail.getPageNum());
		params.put("pageSize", detail.getPageSize());

		try {

			String resultStr = HttpClientUtil.doPost(centerUrl
					+ centerScoreDetailListUrl + id, params);
			Result list = new Gson().fromJson(resultStr, Result.class);

			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public ExamineeScoresDetail saveByEntity(ExamineeScoresDetail detail) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uniquecode", uniquecode);

		try {
			if (detail.getExamineeScoresId() != null) {
				params.put("examineeScoresId", detail.getExamineeScoresId());
			}

			if (detail.getExamInfoId() != null) {
				params.put("examInfoId", detail.getExamInfoId());
			}

			if (detail.getqId() != null && !"".equals(detail.getqId())) {
				params.put("qId", detail.getqId());
			}

			if (detail.getsNo() != null && !"".equals(detail.getsNo())) {
				params.put("sNo", detail.getsNo());
			}

			if (detail.getqStem() != null && !"".equals(detail.getqStem())) {
				params.put("qStem", detail.getqStem());
			}

			if (detail.getqType() != null) {
				params.put("qType", detail.getqType());
			}

			if (detail.getqTypeClassify() != null) {
				params.put("qTypeClassify", detail.getqTypeClassify());
			}

			if (detail.getQueNo() != null) {
				params.put("queNo", detail.getQueNo());
			}

			if (detail.getQueId() != null) {
				params.put("queId", detail.getQueId());
			}

			if (detail.getOperationId() != null
					&& !"".equals(detail.getOperationId())) {
				params.put("operationId", detail.getOperationId());
			}

			if (detail.getqRightAnswer() != null
					&& !"".equals(detail.getqRightAnswer())) {
				params.put("qRightAnswer", detail.getqRightAnswer());
			}

			if (detail.getqExamineeAnswer() != null
					&& !"".equals(detail.getqExamineeAnswer())) {
				params.put("qExamineeAnswer", detail.getqExamineeAnswer());
			}

			if (detail.getqScore() != null) {
				params.put("qScore", detail.getqScore());
			}

			if (detail.getqTopicScore() != null) {
				params.put("qTopicScore", detail.getqTopicScore());
			}

			String resultStr = HttpClientUtil.doPost(centerUrl
					+ centerScoreDetailSaveUrl, params);
			GsonBuilder builder = new GsonBuilder();

			builder.registerTypeAdapter(Date.class,
					new JsonDeserializer<Date>() {
						@Override
						public Date deserialize(JsonElement json, Type typeOfT,
								JsonDeserializationContext context)
								throws JsonParseException {
							return new Date(json.getAsJsonPrimitive()
									.getAsLong());
						}
					});

			ResultForExamineeScoresDetail result = builder.create().fromJson(
					resultStr, ResultForExamineeScoresDetail.class);

			if (result.getCode() == 200) {

				return result.getData();
			} else {

				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Result saveByClientEntity(ExamineeScoreDetailEntity entity) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uniquecode", uniquecode);

		try {
			Result testConn = systemService.TestConn();
			List<ExamineeScoresDetail> list = entity.getList();
			if (testConn.getCode() == 200) {
				params.put("list", entity.getList());

				String resultStr = HttpClientUtil.doPost(centerUrl
						+ centerScoreDetailClientSaveUrl, params);

				Result result = new Gson().fromJson(resultStr, Result.class);

				// return result;
				for (ExamineeScoresDetail detail : list) {
					detail.setField4(1);
				}

			} else {
				for (ExamineeScoresDetail detail : list) {
					detail.setField4(0);
				}
			}

			DeliveryExamineeScoresDetail dScore = null;
			for (ExamineeScoresDetail detail : list) {
				dScore = new DeliveryExamineeScoresDetail();
				dScore.setId(Tool.getUUID());
				dScore.setCreateTime(new Date());
				dScore.setExamInfoId(detail.getExamInfoId());
				dScore.setOperationId(detail.getOperationId());
				dScore.setqId(detail.getqId());
				dScore.setqStem(detail.getqStem());
				dScore.setqTopicScore(detail.getqTopicScore());
				dScore.setqType(detail.getqType());
				dScore.setqTypeClassify(detail.getqTypeClassify());
				dScore.setQueId(detail.getQueId());
				dScore.setQueNo(detail.getQueNo());
				dScore.setsNo(detail.getsNo());
				dScore.setField4(detail.getField4());

				List<DeliveryExamineeScoresDetail> sScores = deliveryExamineeScoresDetailMapper
						.selectByParams(dScore);

				if (sScores.size() > 0) {
					dScore.setId(sScores.get(0).getId());
					dScore.setCreateTime(sScores.get(0).getCreateTime());
					dScore.setUpdateTime(new Date());

					deliveryExamineeScoresDetailMapper
							.updateByPrimaryKey(dScore);
				} else {

					deliveryExamineeScoresDetailMapper.insert(dScore);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

			return new Result(8106, "保存出错了！");
		}

		return new Result(200, "保存成功！");
		/*
		 * 
		 * Map<String, Object> params = new HashMap<String, Object>();
		 * params.put("uniquecode", uniquecode); DeliveryExamineeScoresDetail
		 * scoreDetail = new DeliveryExamineeScoresDetail();
		 * 
		 * try { if (detail.getExamineeScoresId() != null) {
		 * params.put("examineeScoresId", detail.getExamineeScoresId());
		 * scoreDetail.setExamineeScoresId(detail.getExamineeScoresId()); }
		 * 
		 * if (detail.getExamInfoId() != null) { params.put("examInfoId",
		 * detail.getExamInfoId());
		 * scoreDetail.setExamInfoId(detail.getExamInfoId()); }
		 * 
		 * if (detail.getqId() != null && !"".equals(detail.getqId())) {
		 * params.put("qId", detail.getqId());
		 * scoreDetail.setqId(detail.getqId()); }
		 * 
		 * if (detail.getsNo() != null && !"".equals(detail.getsNo())) {
		 * params.put("sNo", detail.getsNo());
		 * scoreDetail.setsNo(detail.getsNo()); }
		 * 
		 * if (detail.getqStem() != null && !"".equals(detail.getqStem())) {
		 * params.put("qStem", detail.getqStem());
		 * scoreDetail.setqStem(detail.getqStem()); }
		 * 
		 * if (detail.getqType() != null) { params.put("qType",
		 * detail.getqType()); scoreDetail.setqType(detail.getqType()); }
		 * 
		 * if (detail.getqTypeClassify() != null) { params.put("qTypeClassify",
		 * detail.getqTypeClassify());
		 * scoreDetail.setqTypeClassify(detail.getqTypeClassify()); }
		 * 
		 * if(detail.getQueNo() != null) { params.put("queNo",
		 * detail.getQueNo()); scoreDetail.setQueNo(detail.getQueNo()); }
		 * 
		 * if (detail.getQueId() != null) { params.put("queId",
		 * detail.getQueId()); scoreDetail.setQueId(detail.getQueId()); }
		 * 
		 * if (detail.getOperationId() != null &&
		 * !"".equals(detail.getOperationId())) { params.put("operationId",
		 * detail.getOperationId());
		 * scoreDetail.setOperationId(detail.getOperationId()); }
		 * 
		 * if (detail.getqRightAnswer() != null &&
		 * !"".equals(detail.getqRightAnswer())) { params.put("qRightAnswer",
		 * detail.getqRightAnswer());
		 * scoreDetail.setqRightAnswer(detail.getqRightAnswer()); }
		 * 
		 * if (detail.getqExamineeAnswer() != null &&
		 * !"".equals(detail.getqExamineeAnswer())) {
		 * params.put("qExamineeAnswer", detail.getqExamineeAnswer());
		 * scoreDetail.setqExamineeAnswer(detail.getqExamineeAnswer()); }
		 * 
		 * if (detail.getqScore() != null) { params.put("qScore",
		 * detail.getqScore()); scoreDetail.setqScore(detail.getqScore()); }
		 * 
		 * if (detail.getqTopicScore() != null) { params.put("qTopicScore",
		 * detail.getqTopicScore());
		 * scoreDetail.setqTopicScore(detail.getqTopicScore()); }
		 * 
		 * Result testConn = systemService.TestConn(); if (testConn.getCode() ==
		 * 200) { String resultStr = HttpClientUtil.doPost(centerUrl +
		 * centerScoreDetailSaveUrl, params); GsonBuilder builder = new
		 * GsonBuilder();
		 * 
		 * builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>()
		 * {
		 * 
		 * @Override public Date deserialize(JsonElement json, Type typeOfT,
		 * JsonDeserializationContext context) throws JsonParseException {
		 * return new Date(json.getAsJsonPrimitive().getAsLong()); } });
		 * 
		 * ResultForExamineeScoresDetail result =
		 * builder.create().fromJson(resultStr,
		 * ResultForExamineeScoresDetail.class);
		 * 
		 * if (result.getCode() == 200) {
		 * 
		 * return result.getData(); } else {
		 * 
		 * return null; }
		 * 
		 * 
		 * 
		 * } else {
		 * 
		 * }
		 * 
		 * 
		 * } catch (Exception e) { e.printStackTrace(); return null; }
		 */}

	public Result syncScoreDetail(DeliveryExamineeScoresDetail detailEntity) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uniquecode", uniquecode);

		try {
			if (detailEntity.getExamineeScoresId() != null) {
				params.put("examineeScoresId", detailEntity.getExamineeScoresId());
			}

			if (detailEntity.getExamInfoId() != null) {
				params.put("examInfoId", detailEntity.getExamInfoId());
			}

			if (detailEntity.getqId() != null && !"".equals(detailEntity.getqId())) {
				params.put("qId", detailEntity.getqId());
			}

			if (detailEntity.getsNo() != null && !"".equals(detailEntity.getsNo())) {
				params.put("sNo", detailEntity.getsNo());
			}

			if (detailEntity.getqStem() != null && !"".equals(detailEntity.getqStem())) {
				params.put("qStem", detailEntity.getqStem());
			}

			if (detailEntity.getqType() != null) {
				params.put("qType", detailEntity.getqType());
			}

			if (detailEntity.getqTypeClassify() != null) {
				params.put("qTypeClassify", detailEntity.getqTypeClassify());
			}

			if (detailEntity.getQueNo() != null) {
				params.put("queNo", detailEntity.getQueNo());
			}

			if (detailEntity.getQueId() != null) {
				params.put("queId", detailEntity.getQueId());
			}

			if (detailEntity.getOperationId() != null
					&& !"".equals(detailEntity.getOperationId())) {
				params.put("operationId", detailEntity.getOperationId());
			}

			if (detailEntity.getqRightAnswer() != null
					&& !"".equals(detailEntity.getqRightAnswer())) {
				params.put("qRightAnswer", detailEntity.getqRightAnswer());
			}

			if (detailEntity.getqExamineeAnswer() != null
					&& !"".equals(detailEntity.getqExamineeAnswer())) {
				params.put("qExamineeAnswer", detailEntity.getqExamineeAnswer());
			}

			if (detailEntity.getqScore() != null) {
				params.put("qScore", detailEntity.getqScore());
			}

			if (detailEntity.getqTopicScore() != null) {
				params.put("qTopicScore", detailEntity.getqTopicScore());
			}

			String resultStr = HttpClientUtil.doPost(centerUrl + centerScoreDetailSaveUrl, params);
			GsonBuilder builder = new GsonBuilder();

			builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
				@Override
				public Date deserialize(JsonElement json,
						Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
								
					return new Date(json.getAsJsonPrimitive().getAsLong());
				}
			});

			ResultForExamineeScoresDetail result = builder.create().fromJson(resultStr, ResultForExamineeScoresDetail.class);
			
			if (result.getCode() == 200) {
				return new Result();
			} else {

				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
}
